<?php namespace App\Http\Controllers\Api\Admin;

use App\Forum;
use App\ForumDiscuss;
use App\Http\Controllers\Controller;
use App\User;
use Illuminate\Contracts\Auth\Guard;
use Illuminate\Http\Request;
use Auth;

class ForumController extends Controller
{
    public function setForum(Request $request,Guard $guard){
        if(Auth::guard('admin')->guest())
            return response()->json(['level'=>'error','message'=>'请先登录']);

        $title   = $request->get("title");
        $desc    = $request->get("desc");
        $content = $request->get("content");
        $imgId   = $request->get("img_id");
        $readNum = $request->get("read_num");
        $isEssence = $request->get("is_essence");
        $id        = $request->get("id");

        /** @var Forum $thatForum */
        $thatForum = Forum::whereId($id)->first();
        if(!$thatForum){
            $thatForum = new Forum();
            $thatForum->user_id = 913;
        }

        if(!is_null($title))$thatForum->title = $title;
        if(!is_null($desc))$thatForum->desc = $desc;
        if(!is_null($content))$thatForum->content = $content;
        if(!is_null($imgId))$thatForum->img_id = $imgId;
        if(!is_null($readNum))$thatForum->read_num = $readNum;
        if(!is_null($isEssence))$thatForum->is_essence = $isEssence;
        $thatForum->save();

        return response()->json(['level'=>'success','message'=>trans('创建成功'),'data'=>$thatForum]);
    }

    public function delForum(Request $request,Guard $guard){
        if(Auth::guard('admin')->guest())
            return response()->json(['level'=>'error','message'=>'请先登录']);

        $id = !is_null($request->get('id'))?$request->get('id'):0;

        /** @var Forum $plan */
        $plan = Forum::whereId($id)->first();

        if(!$plan)
            return response()->json(["level"=>"error","message"=>"该讨论不存在！"],203);

        $plan->delete();

        return response()->json(['level'=>'success','message'=>'删除成功']);
    }


    public function setForumDiscuss(Request $request,Guard $guard){

        if(Auth::guard('admin')->guest())
            return response()->json(['level'=>'error','message'=>'请先登录']);

        $forum_discuss_id = !is_null($request->get('forum_id'))?$request->get('forum_id'):0;

        $forum_id=$request->get('forum_id');

        $nick_name=$request->get('nick_name');

        $photo_id=$request->get('photo_id');

        $content=$request->get('content');


       /* $forumDiscuss=ForumDiscuss::whereId($forum_discuss_id)->first();

        if(!$forumDiscuss)*/
            $forumDiscuss=new ForumDiscuss();
        if(!is_null($forum_id))$forumDiscuss->f_id=$forum_id;
        if(!is_null($nick_name))$forumDiscuss->nick_name=$nick_name;
        if(!is_null($content))$forumDiscuss->content =$content;
        if(!is_null($photo_id))$forumDiscuss->photo_id=$photo_id;

        $forumDiscuss->save();

        return response()->json(['level'=>'success','message'=>'添加成功']);
    }





    public function delForumDiscuss(Request $request,Guard $guard){
        if(Auth::guard('admin')->guest())
            return response()->json(['level'=>'error','message'=>'请先登录']);

        $id = !is_null($request->get('id'))?$request->get('id'):0;

        /** @var ForumDiscuss $plan */
        $plan = ForumDiscuss::whereId($id)->first();

        if(!$plan)
            return response()->json(["level"=>"error","message"=>"该讨论不存在！"],203);

        $plan->delete();

        return response()->json(['level'=>'success','message'=>'删除成功']);
    }
}